package pe.com.ecm_ws;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;

public class WsClient {

	public static int TIME_OUT = 999999999;

	public static void main(String[] args) throws Exception {

		try {
			
			Map<String, Object>       objRequestContext       = null;
			BindingProvider            objBindingProvider            = null;
			String vUsuarioWssIN  = "lcastro";
			String vPasswordWssIN = "12345678";
			
			Map<String, List<String>> objListaCabeceraRequest = null; 
			
			IntegracionECMService objDummyWsSecurityImplService = new IntegracionECMService();
			IntegracionECMImpl objDummyWsSecurityImplPort = objDummyWsSecurityImplService.getIntegracionECMPort();
			
			objBindingProvider = (BindingProvider)objDummyWsSecurityImplPort;
			objRequestContext = ((BindingProvider)objDummyWsSecurityImplPort).getRequestContext(); 	
			
			objBindingProvider.getRequestContext().put( BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8080/integracionecm/ecm-ws?wsdl" );
			

			objBindingProvider.getRequestContext().put("com.sun.xml.internal.ws.connect.timeout",
					TIME_OUT);
			objBindingProvider.getRequestContext().put("com.sun.xml.internal.ws.request.timeout",
					TIME_OUT);
			objBindingProvider.getRequestContext().put("com.sun.xml.ws.request.timeout", TIME_OUT);
			objBindingProvider.getRequestContext().put("com.sun.xml.ws.connect.timeout", TIME_OUT);
			objBindingProvider.getRequestContext().put("com.sun.xml.internal.ws.connect.timeout",
					TIME_OUT);
			objBindingProvider.getRequestContext().put("com.sun.xml.internal.ws.request.timeout",
					TIME_OUT);
			objBindingProvider.getRequestContext().put("javax.xml.ws.client.connectionTimeout",
					TIME_OUT);
			objBindingProvider.getRequestContext().put("javax.xml.ws.client.receiveTimeout", TIME_OUT);

			/*Retorno retorno = si.createDirectory("/LIBROS");
			System.out.println(retorno.getCodigo());
			System.out.println(retorno.getMensaje());

			Path path = Paths
					.get("D://lacastro//documentos//manuales//ActiveMQInAction.pdf");
			byte[] file = Files.readAllBytes(path);

			retorno = si.createDocument(file, "/LIBROS", "ActiveMQInAction",
					"PDF");
			System.out.println(retorno.getCodigo());
			System.out.println(retorno.getMensaje());
			System.out.println(retorno.getId());

			String uuid = retorno.getId();

			retorno = si.getDocument(uuid);
			System.out.println(retorno.getCodigo());
			System.out.println(retorno.getMensaje());
			System.out.println(retorno.getBytes());

			FileOutputStream fos = new FileOutputStream(
					"C://tmp//descarga3.pdf");
			fos.write(retorno.getBytes());
			fos.close();

			retorno = si.deleteDocument(uuid);
			System.out.println(retorno.getCodigo());
			System.out.println(retorno.getMensaje());

			retorno = si.deleteDirectory("/LIBROS");
			System.out.println(retorno.getCodigo());
			System.out.println(retorno.getMensaje());*/
			
			
			objListaCabeceraRequest = new HashMap<String, List<String>>();
			   
		    objListaCabeceraRequest.put( "username", Collections.singletonList( vUsuarioWssIN  ) );
		    objListaCabeceraRequest.put( "Password", Collections.singletonList( vPasswordWssIN ) );
		   
		    objRequestContext.put( MessageContext.HTTP_REQUEST_HEADERS, objListaCabeceraRequest );
		    //---------------------------------------------------------------------//
			
			//PROCESAR WS:
		    Retorno retorno = objDummyWsSecurityImplPort.queryForContent("luis");
		    //Retorno retorno = objDummyWsSecurityImplPort.existDirectory("/LIBROS");
			System.out.println(retorno.getCodigo());
			System.out.println(retorno.getMensaje());
			
			
			

		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
